/**
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* Only one allowed */
if (typeof Msg == "undefined")
{
    /**
     * Constructor for Messages passed between mprocs
     */
    function Msg()
    {
        Msg._idSeqNum += 1;
        this._id = Msg._idSeqNum;
        this.next = null;
        this.remote = false;
        this.dstId = 0;
        this.dstSubId = 0;
        this.srcId = 0;
        this.srcSubId = 0;
        this.guid = 0;
        this.cmd = 0;
        this.tag = 0;
        this.timeout = 0;
        this.status = 0;
        this.arg1 = 0;
        this.arg2 = 0;
        this.arg3 = 0;
        this.arg4 = 0;

        this.obj = null;

        debug("Msg: _id " + this._id);
    }
    Msg._idSeqNum = 0;
    Msg.SERDES_ID = 2;
    
    Msg.prototype.is = function(guid, cmd) {
        return ((this.guid == guid) && (this.cmd == cmd));
    }
    
    Msg.prototype.isRequest = function() {
        return ((this.guid == guid) && (this.cmd == cmd));
    }
    
    
    function _deser(jo) {
        // This is recursive msg must be a var
        var msg = new Msg();
        msg.remote = jo[0];
        msg.dstId = jo[1];
        msg.dstSubId = jo[2];
        msg.srcId = jo[3];
        msg.srcSubId = jo[4];
        msg.guid = jo[5];
        msg.cmd = jo[6];
        msg.tag = jo[7];
        msg.timeout = jo[8];
        msg.status = jo[9];
        msg.arg1 = jo[10];
        msg.arg2 = jo[11];
        msg.arg3 = jo[12];
        msg.arg4 = jo[13];
        msg.obj = Deserializer._deser(jo[14]);
        return msg;
    }
    Deserializer.register(Msg.SERDES_ID, _deser);
        
    Msg.prototype.toJSONString = function() {
        return "{ \"" + Msg.SERDES_ID + "\":["
         + this.remote + ","
         + this.dstId + ","
         + this.dstSubId + ","
         + this.srcId + ","
         + this.srcSubId + ","
         + this.guid + ","
         + this.cmd + ","
         + this.tag + ","
         + this.timeout + ","
         + this.status + ","
         + this.arg1 + ","
         + this.arg2 + ","
         + this.arg3 + ","
         + this.arg4 + ","
         + (this.obj ? this.obj.toJSONString() : "null")
         + "]}"
    }

}
